////////////////////////
// Constants &	      //
// globals	      //
////////////////////////

var _clocks = new Array();
var _currentClock = 0;

//////////////////////
// Utilities	    //
//////////////////////

function LoadClocks() {
  // unserialize options into clocks
  options.defaultValue("clocks") = -1;

  if (options("clocks") == -1) {
    NewAlarm();
    NewTimer();
    NewStopwatch();
    return;
  }
  var i = 0;
  for (i = 0; i < options("clocks"); i++) {
    var c = NewAlarm();
    c.Load(i);
  }
}

function SaveClocks() {
  // serialize clocks into options
  options("clocks") = _clocks.length;
  var i = 0;
  for (i = 0; i < _clocks.length; i++)
    _clocks[i].Save(i);
}

function GetClockId(item) {
  var l = GetClockList();
  var i;
  for (i = 0; i < l.length; i++)
    if (l[i] == item)
      return i;
  return -1;
}

// Array of strings with info about each clocks
function GetClockList() {
  var l = new Array();
  var i;
  for (i = 0; i < _clocks.length; i++)
    l.push(_clocks[i].ListName());
  return l;
}

// Insert a clock in the clock list in a sorted way
function InsertClock(item) {
  var i;
  for (i = 0; i < _clocks.length; i++)
    if (_clocks[i].type == item.type) {
      _clocks.splice(i, 0, item)
      return;
    }
  _clocks.unshift(item);
}

function NewAlarm() {
  var newClock = new Clock();
  newClock.type = 0;
  newClock.SetName(B_ADD_A);
  newClock.duration = "";
  InsertClock(newClock);
  return newClock;
}

function NewTimer() {
  var newClock = new Clock();
  newClock.type = 1;
  newClock.SetName(B_ADD_T);
  newClock.alarm = "";
  InsertClock(newClock);
  return newClock;
}
function NewStopwatch() {
  var newClock = new Clock();
  newClock.type = 2;
  newClock.SetName(B_STOPWATCH);
  newClock.alarm = "";
  newClock.duration = "";
  InsertClock(newClock);
  return newClock;
}


///////////////////
// Clock  	 //
// class	 //
///////////////////

function Clock() {
  this.type = 1; // 0=alarm, 1=timer, 2=stopwatch
  this.sound = 0;
  this.active = true;
  this.name = "";
  this.command = "";

  // alarm only
  this.alarm = "23:00:00";
  this.repeat = 0; // 0=none, 1=day, 2=week

  // timer only
  this.duration = "00:10:00"; // in seconds
  this.time = 0;
  this.state = 0; // 0=stop, 1=play, 2=pause
}

Clock.prototype.SetName = function (name, n) {
  if (n == undefined)
    n = 0;
  var i;
  var tmp = name+n;
  if (n == 0)
    tmp = name;
  for (i = 0; i < _clocks.length; i++) {
    if (_clocks[i] == this)
      continue;
    if (_clocks[i].name == tmp) {
      return this.SetName(name, n+1);
    }
  }
  this.name = tmp;
}

Clock.prototype.Save = function (id) {
  options("clock"+id+"name") = this.name;
  options("clock"+id+"type") = this.type;
  options("clock"+id+"sound") = this.sound;
  options("clock"+id+"active") = this.active;
  options("clock"+id+"command") = this.command;
  options("clock"+id+"alarm") = this.alarm;
  options("clock"+id+"repeat") = this.repeat;
  options("clock"+id+"duration") = this.duration;
  if (this.time != 0) {
    options("clock"+id+"time") = formatDate(this.time, options("timeFormat"));
  } else {
    options("clock"+id+"time") = this.time;
  }
  options("clock"+id+"state") = this.state;
};

Clock.prototype.Load = function (id) {
  this.name = options("clock"+id+"name");
  this.type = options("clock"+id+"type");
  this.sound = options("clock"+id+"sound");
  this.active = options("clock"+id+"active");
  this.command = options("clock"+id+"command");
  this.alarm = options("clock"+id+"alarm");
  this.repeat = options("clock"+id+"repeat");
  this.duration = options("clock"+id+"duration");
  if (options("clock"+id+"time") == 0) {
    this.time = options("clock"+id+"time");
  } else {
    this.time = StringToDate(options("clock"+id+"time"));
  }
  this.state = options("clock"+id+"state");
};

// Return the string of the clock as it appears in the list
Clock.prototype.ListName = function () {
  var name = this.name;
//   if (this.name.lenght > 15) {
//     name = this.name.substr(0, 15);
//   }
  if (this.type == 0) {
    return A_ALARM+": "+name+", "+this.alarm;
  } else if (this.type == 1) {
    return A_TIMER+": "+name+", "+this.duration;
  } else if (this.type == 2) {
    return A_STOPWATCH+": "+name+" ";
  }
};

Clock.prototype.GetDate = function () {
  if (this.type == 0) { // alarm
    return StringToDate(this.alarm);
  }
  if (this.time == 0) {
    return StringToDate("0:0:0");
  }
  return this.time;
};

Clock.prototype.Check = function() {
  if (this.active == false) {
    return;
  }
  if (this.type == 0) { // alarm
    var a = StringToDate(this.alarm);
    var now = new Date();
    if (a.getHours() != now.getHours() ||
	a.getMinutes() != now.getMinutes() ||
	a.getSeconds() != now.getSeconds()) {
      return;
    }

  } else if (this.type == 1) { // timer
    if (this.time == 0) {
      this.time = StringToDate(this.duration);
      this.state = 0;
      DrawDetails();
    }
    if (this.state == 2 || this.state == 0)
      return;
    this.time.setSeconds(this.time.getSeconds() - 1);
    if (this.time.getHours() != 0 ||
	this.time.getMinutes() != 0 ||
	this.time.getSeconds() != 0) {
      return;
    }

  } else if (this.type == 2) { // stopwatch
    if (this.time == 0) {
      this.time = StringToDate("0:0:0");
      this.state = 0;
      DrawDetails();
    }
    if (this.state == 2 || this.state == 0)
      return;
    this.time.setSeconds(this.time.getSeconds() + 1);
    return;
  }
  if (options("sound") != "-")
    PlaySound(GetAbsolutePath(options("sound")+".wav"), 4);
  this.time = 0;
  if (this.command.length != "")
    Execute(this.command);
  setTimeout("Alert(\""+this.name+"!\")", 100); // this helps fix a weird bug..
}

function StringToDate(s) {
    // Parse the date
    var re = /(\d+).(\d+).(\d+)/;
    var m = re.exec(s);
    // set the date
    var hours = 0;
    var minutes = 0;
    var seconds = 0;
    if (m == null) {
      re = /(\d+).(\d+)/;
      m = re.exec(s);
    } else {
      seconds = m[3];
    }
    if (m != null) {
      hours = m[1];
      minutes = m[2];
    }
    var d = new Date();
    d.setHours(hours);
    d.setMinutes(minutes);
    d.setSeconds(seconds);
    return d;
}
